package com.wjf.birdfish.commons;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.wjf.birdfish.action.BaseAction;
import com.wjf.birdfish.util.SystemProperties;

public class DownLoadAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6430211549084002458L;
	// 下载文件原始存放路径
	// 文件名参数变量
	private String fileName;
	private String fileChName;

	public String getFileChName() {
		return fileChName;
	}

	public void setFileChName(String fileChName) {

		this.fileChName = fileChName;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		//fileChName = "操作日志-";
		fileName = fileName.substring(fileName.indexOf("-") + 1);
		this.fileName = fileName;
	}

	// 从下载文件原始存放路径读取得到文件输出流
	public InputStream getDownloadFile() {
		InputStream inputStream = null;

		SystemProperties systemProperties = new SystemProperties();
		String fileSeparator = systemProperties.getFileSeparator();
		String filePath = ServletActionContext.getServletContext().getRealPath(
				"")
				+ fileSeparator + "systemmng" + fileSeparator;
		try {
			inputStream = new FileInputStream(filePath + fileName);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return inputStream;
	}

	// 如果下载文件名为中文，进行字符编码转换
	public String getDownloadChineseFileName() {

//		String agent = request.getHeader("User-Agent");
//		boolean isMSIE = (agent != null && agent.indexOf("MSIE") != -1);
//
//		try {
//			if (isMSIE) {
//				fileChName = URLEncoder.encode(fileChName, "UTF-8");
//			} else {
//				fileChName = new String(fileChName.getBytes("UTF-8"),
//						"ISO-8859-1");
//			}
//		} catch (UnsupportedEncodingException e) {
//			e.printStackTrace();
//		}

		String downloadChineseFileName = fileName;
		return  downloadChineseFileName;
	}

	public String execute() {
		return SUCCESS;
	}

	public void prepare() throws Exception {
	}
}